<script type="text/javascript">

var paginationTotal =<{$total}>;//记录总数
$(function() {
	
//	initData(0);
	clear();
        
	$("#confirmDialog").dialog({
		autoOpen:false,
		modal:true,
		show:"slide",
		buttons:{
			"<{$lang.ok}>":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$("#add").dialog({
		autoOpen:false,
        modal:true,
        width:380,
        height:450,
        show:"slide",
       	buttons: {
       		'<{$lang.ok}>': function() {
          		save();
            },
            '<{$lang.cancel}>': function() {
             	$(this).dialog('close');
           	}
    	}
	});
	//回车处理
	$("#customer_id_sch").keyup(function(e){
		var key = e.which;
		if(key==13){
			initData(0);
		}
	});
});

    function loadData(page, pageSize) {
        var status = $('[name=search_status]').val();
        var customer_id_sch = $('[name=customer_id_sch]').val();
        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "/customer/customer/list", //请求的处理url
            data:{
                'page':page,'pageSize':pageSize,
                'status':status,'customer_id':customer_id_sch
            },

            success: function(json) {
                var list = "";
                paginationTotal = json.total;
                if (json.ask != 1) {
                    list = "<td colspan='10' class=''>&nbsp;<{$lang.no_data}></td>";
                } else {
                    var i = paginationCurrentPage == 1 ? 1 : paginationPageSize * (paginationCurrentPage - 1) + 1;
                    $.each(json.result, function(key, val) {
                        if ((key + 1) % 2 == 1) {
                            list += "<tr class=''>";
                        } else {
                            list += "<tr class='even-tr'>";
                        }
                        list += "<td >" + val.customer_id + "</td>";
                        list += "<td >" + val.customer_firstname + "</td>";
                        list += "<td >" + val.customer_lastname + "</td>";
                        list += "<td >" + val.customer_email + "</td>";

                        list += "<td >" + val.customer_telephone + "</td>";
                        var statuStr;
                        var languages = (val.languages_id == '1') ? '<{$lang.chinese}>' : 'English';
                        if (val.customer_status == '1') {
                            statuStr = "<img src='/images/icon/statusok.png' title='<{$lang.activated}>'>";
                        } else {
                            statuStr = "<img src='/images/icon/unnormal.gif' title='<{$lang.unactivate}>'>";
                        }
                        list += "<td >" + statuStr + "</td>";

                        list += "<td >" + val.customer_last_update + "</td>";
                        list += "<td >" + val.customer_date_reg + "</td>";

                        list += "<td >" + languages + "</td>";
                        list += "<td>";
                        //list += "<a href=\"javascript:getplan('" + val.customer_id + "')\"><img src='/images/icon/info.jpg' title='查看套餐<{$lang.msg}>'></a>&nbsp;&nbsp;|&nbsp;&nbsp;";
                        list += "<a href=\"javascript:editCustomer('" + val.customer_id + "')\"><img src='/images/icon/edit.gif' title='<{$lang.edit}><{$lang.user}><{$lang.msg}>'></a>";
                        list += "</td>";
                        list += "</tr>";
                    });
                }
                $("#list").html(list);
            }
        });
    }


    //<{$lang.add}><{$lang.edit}><{$lang.user}><{$lang.msg}>
    function editCustomer(id) {
        clear();
        if (id) {
            $.ajax({
                type: "POST",
                async: false,
                dataType: "json",
                url: "/customer/customer/single", //请求的处理url
                data:{'id':id},
                success: function(json) {
                    if (json.ask == 1) {
                        var val = json.result;
                        $('[name=customer_id]').val(val.customer_id);
                        $('[name=customer_name]').val(val.customer_name);
                        $('[name=customer_first_name]').val(val.customer_firstname);
                        $('[name=customer_last_name]').val(val.customer_lastname);
                        $('[name=customer_email]').val(val.customer_email);

                        $('[name=customer_telephone]').val(val.customer_telephone);
                        $('[name=customer_fax]').val(val.customer_fax);
                        $('[name=customer_status]').val(val.customer_status);
                        $('[name=customer_company_name]').val(val.customer_company_name);
                        $('[name=languages_id]').val(val.languages_id);
                    }
                    $("#add").dialog("open");
                }
            });
        } else {
            $("#add").dialog("open");
        }
    }

    function save() {
        vForm = '';
        vForm = $("#createForm").serializeArray();
        var tip = '';
        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "/customer/customer/edit", //请求的处理url
            data:vForm,
            success: function(json) {
                if (json.ask) {
                    tip = '<{$lang.operation}><{$lang.success}>';
                } else {
                    tip = '<{$lang.operation}><{$lang.fail}>';
                }
                loadData(paginationCurrentPage, paginationPageSize);
                $("#add").dialog("close");
                $("#confirmDialog").html(tip).dialog("open");
            }
        });
    }

    function search() {
        initData(0);
    }

    function clear() {
        $('[name=search_status]').val('');
        $('[name=username]').val('');

        $('[name=customer_id]').val('');
        $('[name=customer_name]').val('');
        $('[name=customer_first_name]').val('');
        $('[name=customer_last_name]').val('');
        $('[name=customer_email]').val('');

        $('[name=customer_telephone]').val('');
        $('[name=customer_fax]').val('');
        $('[name=customer_status]').val('');
        $('[name=customer_company_name]').val('');
        $('[name=languages_id]').val('');
    }

    //查看套餐使用情况
    function getplan(c_id) {
        if (c_id == '') {
            return false;
        }
        var url = '/customer/customer/getplan/customer_id/' + c_id;
        window.parent.$('<iframe id="ifr_order_product_list" name="ifr_order_product_list" src="' + url + '" />').dialog({
            title: '<{$lang.plan}><{$lang.detail}>',
            autoOpen: true,
            width: 760,
            modal: false,
            resizable: false,
            position:'top',
            autoResize: true,
            close:function() {
                $(this).remove();
            },
            buttons: {
                'Close/<{$lang.close}>': function() {
                    $(this).remove();
                }
            }
        }).width(730).height(500);
    }

</script>
<style type="text/css">
    #iTitle div {
        display: inline-block;
        float: left;
        padding: 0 0 0 10px;
        margin-bottom: 12px;
    }

    .mySelect {
        height: 22px;
        border: 1px solid #CCCCCC;
    }

    #iTitle input {
        height: 20px;
        border: 1px solid #CCCCCC;
    }
</style>

<div class="search_div">
    <div id="iTitle">
        <form id="seach-form" onsubmit="return false;" action="" method="post" name="seach-form">

            <div class="tDiv4" style="padding-left:10px"><{$lang.status}>：</div>
            <div class="tDiv5">
                <select id="search_status" class="mySelect" name="search_status">
                    <option value=''><{$lang.all}></option>
                    <option value='0'><{$lang.unactivate}></option>
                    <option value='1'><{$lang.activated}></option>
                    <option value='2'><{$lang.stop}></option>
                </select>
            </div>

            <div class="tDiv6"><{$lang.customer_code}>：</div>
            <div class="tDiv7">
                <input id="customer_id_sch" class="myInput ui-autocomplete-input" type="text" value="" name="customer_id_sch" />
            </div>

            <div class="tDiv8">
                <input id="searchSubmit" class="myButton" type="button" onclick="search();" value="<{$lang.search}>">
            </div>
            <!--<div class="tDiv1" permission="adduser">
                   <input class="myButton" type="button" value="<{$lang.add}>" onclick="editCustomer('')">
               </div>-->
            <div class="myClear"></div>

        </form>
    </div>
</div>

<div id="message" style="clear:both;color:red;"></div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">
    <thead>
    <tr>
        <th><{$lang.customer_code}></th>
        <th><{$lang.first_name}></th>
        <th><{$lang.last_name}></th>
        <th><{$lang.email}></th>

        <th><{$lang.telphone}></th>
        <th><{$lang.status}></th>
        <th><{$lang.last}><{$lang.login}><{$lang.time}></th>
        <th><{$lang.register}><{$lang.date}></th>

        <th><{$lang.default}><{$lang.language}></th>
        <th><{$lang.operation}></th>

    </tr>
    </thead>
    <tbody id="list">

    </tbody>
</table>

<div class="pagination"></div>

<style type="text/css">
    div#add {
        text-align: center;
    }

    div.info {
        text-align: left;
    }

    div.item {
        margin-bottom: 12px;
    }

    input.text {
        height: 22px;
        margin-left: 8px;
    }

    span.text {
        margin-left: 8px;
    }

    span.userInfoLeft {
        display: block;
        float: left;
        width: 100px;
    }
</style>

<div id="add" title="<{$lang.customer}><{$lang.msg}><{$lang.edit}>" style="display:none;">

    <div class="info">
        <div class="validateTips" id="tips"></div>

        <form id="createForm" name="createForm">
			<input type="hidden" name="customer_id" id="customer_id"/>           

            <div class="item">
                <span class="userInfoLeft"><{$lang.first_name}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_first_name" id="customer_first_name"
                       class="text ui-widget-content ui-corner-all"/>
			</span>

                <div style="clear: both;"></div>
            </div>
            <div class="item">
                <span class="userInfoLeft"><{$lang.last_name}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_last_name" id="customer_last_name"
                       class="text ui-widget-content ui-corner-all"/>
			</span>

                <div style="clear: both;"></div>
            </div>

            <div class="item">
                <span class="userInfoLeft"><{$lang.email}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_email" id="customer_email"
                       class="text ui-widget-content ui-corner-all"/>
			</span>

                <div style="clear: both;"></div>
            </div>
            <div class="item">
                <span class="userInfoLeft"><{$lang.telphone}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_telephone" id="customer_telephone"
                       class="text ui-widget-content ui-corner-all"/>
			</span>

                <div style="clear: both;"></div>
            </div>
            <div class="item">
                <span class="userInfoLeft"><{$lang.fax}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_fax" id="customer_fax" class="text ui-widget-content ui-corner-all"/>
			</span>

                <div style="clear: both;"></div>
            </div>
            <div class="item">
                <span class="userInfoLeft"><{$lang.company}><{$lang.name}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_company_name" id="customer_company_name"
                       class="text ui-widget-content ui-corner-all"/>
			</span>

                <div style="clear: both;"></div>
            </div>

            <div class="item">
                <span class="userInfoLeft"><{$lang.status}>:</span>
			<span class="text userInfoRight">
				<select name="customer_status" id="customer_status" class="ui-widget-content ui-corner-all">
                    <option value=''><{$lang.null}></option>
                    <option value='0'><{$lang.unactivate}></option>
                    <option value='1'><{$lang.activated}></option>
                    <option value='2'><{$lang.stop}></option>
                </select> *
			</span>

                <div style="clear: both;"></div>
            </div>
            <div class="item">
                <span class="userInfoLeft"><{$lang.default}><{$lang.language}>:</span>
			<span class="text userInfoRight">
				<select name="languages_id" id="languages_id" class="ui-widget-content ui-corner-all">
                    <option value='1'><{$lang.chinese}></option>
                    <option value='2'>English</option>
                </select> *
			</span>

                <div style="clear: both;"></div>
            </div>

        </form>

    </div>
</div>
<div id="confirmDialog" title="<{$lang.system}><{$lang.msg}>">	</div>